<?php
function view($view,$data = array()){
	// 把下标变成变量名，下标的值变成该变量名的值的方法：
	// 方法1
	// foreach ($data as $key => $value) {
	// 	$$key = $value;
	// }
	//方式2
	$menu = simplexml_load_file('protected/data/menu.xml'); 
	extract($data);
	include(TEMPLATE_PATH.'header.php');
	include(TEMPLATE_PATH.$view.'.php');
	include(TEMPLATE_PATH.'footer.php');
}

function checkLogin(){
	if (empty($_SESSION['username'])) {
		echo '<script>alert("你是非法入侵！请登陆再进来。");window.location.href ="'.BASE_URL.'index.php?admin=1&c=admin&a=login"</script>';
	}
}


/**
 * 返回分页HTML代码
 * 
 * @param unknown $table 表名
 * @param number $curpage	当前页
 * @param number $perpage	每页显示条数
 * @param number $admin
 * @param number $pagenum 显示的页码数
 * @return string
 */
function getPage($table , $curpage = 1, $perpage = 5 , $admin = 0 , $pagenum = 5)
{
	// step1 :得到总的页码数
	$sql = 'select count(*) as count from '.$table;
	$query = mysql_query($sql);
	$row = mysql_fetch_assoc($query);
	$total = $row['count'];							//$count  数据总数
	$totalpage = ceil($total/$perpage);		//$totalpage  页码总数;    ceil()向上取整。
	
	$nextpage = $curpage + 1;
	$perpage = $curpage - 1;
	$floopage = floor($pagenum / 2);
	$starpage = $curpage - $floopage;
	$endpage = $curpage + $floopage;

	if ($totalpage <= $pagenum) 
	{
		$starpage = 1;
		$endpage = $totalpage;
	}
	else
	{
		if ($starpage < 1) 
		{
			$starpage = 1;
			$endpage = $pagenum;
		}
		if ($endpage > $totalpage) 
		{
			$endpage = $totalpage;
			$starpage = $totalpage - $pagenum + 1 ;
		}
	}
	
	$page = '';
	if (!empty($admin)) 
	{
		// 拼接后页码代码
		if($curpage>1)
		{
			$page .= '<a href="'.BASE_URL.'index.php?admin=1&c='.CONTROLLER.'&a=index&p=1" title="首页">首页</a>';
			$page .= '<a href="'.BASE_URL.'index.php?admin=1&c='.CONTROLLER.'&a=index&p='.$perpage.'" title="上一页">&laquo; 上一页</a>';
		}
		if($starpage >= $floopage)
		{
			$page .='...';
		}

		for($i = $starpage; $i <= $endpage; $i++)
		{
			$cur = ($i == $curpage) ? 'current' : '';
			$page .= '<a href="'.BASE_URL.'index.php?admin=1&c='.CONTROLLER.'&a=index&p='.$i.'" class="number '.$cur.'" title="1">'.$i.'</a>';
		}

		if($endpage <= $totalpage-$floopage+1)
		{
			$page .='...';
		}
		if ($curpage<$totalpage) 
		{
			$page .= '<a href="'.BASE_URL.'index.php?admin=1&c='.CONTROLLER.'&a=index&p='.$nextpage.'" title="下一页"> 下一页 &raquo;</a>';
			$page .= '<a href="'.BASE_URL.'index.php?admin=1&c='.CONTROLLER.'&a=index&p='.$totalpage.'" title="尾页">尾页</a>';
		}
		return $page;
	}
	else
	{
		// 拼接前台页码代码
		if($curpage>1)
		{
			$page .= '<a href="'.BASE_URL.'index.php?c='.CONTROLLER.'&a=index&p=1" title="首页">首页</a>';
			$page .= '<a href="'.BASE_URL.'index.php?c='.CONTROLLER.'&a=index&p='.$perpage.'" title="上一页">&laquo; 上一页</a>';
		}
		if($starpage >= $floopage)
		{
			$page .='...';
		}

		for($i = $starpage; $i <= $endpage; $i++)
		{
			$cur = ($i == $curpage)?'current':'';
			$page .= '<a href="'.BASE_URL.'index.php?c='.CONTROLLER.'&a=index&p='.$i.'" class="number '.$cur.'" title="1">'.$i.'</a>';
		}

		if($endpage <= $totalpage-$floopage+1)
		{
			$page .='...';
		}
		if ($curpage<$totalpage) 
		{
			$page .= '<a href="'.BASE_URL.'index.php?c='.CONTROLLER.'&a=index&p='.$nextpage.'" title="下一页"> 下一页 &raquo;</a>';
			$page .= '<a href="'.BASE_URL.'index.php?c='.CONTROLLER.'&a=index&p='.$totalpage.'" title="尾页">尾页</a>';
		}
		
		return $page;
	}
	
}

function upload($file = array(),$type = array(),$size)
{
	$path = 'uploads/';
	$year = date('Y',time());
	$path.=$year.'/';
	if (!file_exists($path))
	{
		mkdir($path); 
	}
	
	$m = date('m',time());
	$path.=$m.'/';
	if (!file_exists($path))
	{
		mkdir($path);
	}
	
	$d = date('d',time());
	$path.=$d.'/';
	if (!file_exists($path))
	{
		mkdir($path);
	}
	
	$file_type = substr($file['image']['name'],strrpos($file['image']['name'],'.'));
	if(!in_array($file_type, $type))
	{
		return false;
	}
	// echo $file['image']['size'];exit();
	if($file['image']['size'] > $size)
	{
		return false;
	}
	
	$file_name = time().'_'.mt_rand(1000,9999).$file_type;
	move_uploaded_file($file['image']['tmp_name'], $path.$file_name);

	return $path.$file_name;
}